package cn.stylefeng.guns.modular.rest.task;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.task.model.params.TaskDetailedParam;
import cn.stylefeng.guns.modular.task.model.result.TaskDetailedResult;
import cn.stylefeng.guns.modular.task.service.TaskDetailedService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 作业明细控制器
 */
@Controller
@RequestMapping("/rest/taskDetailed")
public class restDetailedController extends BaseController {

    @Autowired
    private UserAop userAop;

    @Autowired
    private TaskDetailedService taskDetailedService;

    /**
     * 我的作业明细列表
     */
    @ResponseBody
    @RequestMapping("/melist")
    public LayuiPageInfo list(TaskDetailedParam taskDetailedParam,String condition ) {
    	User user = userAop.getUser();
    	taskDetailedParam.setUserId(user.getUserId());
    	LayuiPageInfo LayuiPageInfo = this.taskDetailedService.findPageBySpecApplets(taskDetailedParam,condition);
    	LayuiPageInfo.setCode(200);
        return LayuiPageInfo;
    }

    /**
     * 作业明细合计列表
     */
    @ResponseBody
    @RequestMapping("/listSum")
    public ResponseData list(TaskDetailedParam taskDetailedParam ) {
        return ResponseData.success(taskDetailedService.findSumListBySpec(taskDetailedParam));
    }

    /**
     * 作业排名
     */
    @ResponseBody
    @RequestMapping("/taskRanking")
    public ResponseData taskRanking(TaskDetailedParam taskDetailedParam ) {
    	User user = userAop.getUser();
    	taskDetailedParam.setDeliverId(user.getDeliverId());
        return ResponseData.success(taskDetailedService.findSumtaskRankingBySpec(taskDetailedParam));
    }

    /**
     * 查询自己作业排名
     */
    @ResponseBody
    @RequestMapping("/metaskRanking")
    public ResponseData metaskRanking(TaskDetailedParam taskDetailedParam ) {
    	User user = userAop.getUser();
    	//确认是否有作业信息
    	taskDetailedParam.setUserId(user.getUserId());
    	taskDetailedParam.setDeliverId(user.getDeliverId());
    	if (taskDetailedService.checkmetaskRanking(taskDetailedParam)) {
    		List<TaskDetailedResult> list =  taskDetailedService.findSumtaskRankingBySpec2(taskDetailedParam);
    		for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getUserId().longValue() == user.getUserId().longValue()) {
					TaskDetailedResult detailedResult = new TaskDetailedResult();
					detailedResult.setV2(i+1+"");
					detailedResult.setV1(list.get(i).getV1());
					detailedResult.setUserName(list.get(i).getUserName());
					if(ToolUtil.isNotEmpty(list.get(i).getHours())) {
						detailedResult.setHours(list.get(i).getHours()+"h");
					} else {
						detailedResult.setHours("0h");
					}
					if(ToolUtil.isNotEmpty(list.get(i).getVolume())) {
						detailedResult.setVolume(list.get(i).getVolume()+"方");
					} else {
						detailedResult.setVolume("0方");
					}
					if(ToolUtil.isNotEmpty(list.get(i).getWeights())) {
						detailedResult.setVolume(list.get(i).getWeights()+"吨");
					} else {
						detailedResult.setWeights("0吨");
					}
					if(ToolUtil.isNotEmpty(list.get(i).getNums())) {
						detailedResult.setNums(list.get(i).getNums()+"件");
					} else {
						detailedResult.setNums("0件");
					}
					return ResponseData.success(detailedResult);
				} 
			}
		} else {
			return ResponseData.success();
		}
		return ResponseData.success();
    }

}
